using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeLancer.HRB.Core.Web.Controls;

namespace FreeLancer.HRB.WebApp
{
  public partial class Login : WebBasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (Request.QueryString.ToString() == "logoff")
      {
        FormsAuthentication.SignOut();
        if (Request.UrlReferrer != null && Request.UrlReferrer != Request.Url)
        {
          Response.Redirect(Request.UrlReferrer.ToString(), true);
        }
        else
        {
          Response.Redirect("Login.aspx");
        }
      }

      if (Page.User.Identity.IsAuthenticated)
      {
        changepassword.Visible = true;
        changepassword.ContinueButtonClick += new EventHandler(changepassword_ContinueButtonClick);
        lsLogout.Visible = true;
        loginControl.Visible = false;
        Page.Title = Resources.labels.changePassword;
      }
      else
      {
        loginControl.LoggedIn += new EventHandler(loginControl_LoggedIn);
        loginControl.FindControl("username").Focus();
      }
    }

    /// <summary>
    /// Handles the ContinueButtonClick event of the changepassword1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void changepassword_ContinueButtonClick(object sender, EventArgs e)
    {
      Response.Redirect(FreeLancer.HRB.Core.Utils.RelativeWebRoot, true);
    }

    /// <summary>
    /// Handles the LoggedIn event of the Login1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void loginControl_LoggedIn(object sender, EventArgs e)
    {
      if (!Roles.IsUserInRole(loginControl.UserName, FreeLancer.HRB.Core.WebAppSettings.Instance.AdministratorRole))
        Response.Redirect(FreeLancer.HRB.Core.Utils.RelativeWebRoot, true);
    }
  }
}
